#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	ps->size = 0;
	ps->capacity = InitCapa;
}

void CheckCapacity(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		SLDateType* tmp = (SLDateType*)realloc(ps->a,sizeof(SLDateType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail:");
			exit(-1);
		}
		ps->capacity *= 2;
		ps->a = tmp;
	}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	CheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
void SeqListPopBack(SeqList* ps)
{
	assert(ps->size);
	ps->size--;
}
void SeqListPopFront(SeqList* ps)
{
	//assert(ps);
	//assert(ps->size);
	//int begin = 0;
	//int end = 1;
	//while (end < ps->size)
	//{
	//	ps->a[begin] = ps->a[end];
	//	begin++;
	//	end++;
	//}
	//ps->size--;
	SeqListErase(ps, 0);
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	//assert(ps);
	//CheckCapacity(ps);
	//int end = ps->size;
	//int begin = ps->size - 1;
	//while (begin >= 0)
	//{
	//	ps->a[end] = ps->a[begin];
	//	begin--;
	//	end--;
	//}
	//ps->a[0] = x;
	//ps->size++;
	SeqListInsert(ps, 0, x);
}
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	CheckCapacity(ps);
	int end = ps->size-1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(ps->size);
	int begin = pos;
	while (begin < ps->size-1)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}
void SeqListPrint(SeqList* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->size = ps->capacity = 0;
	ps->a = NULL;
}